<?php
/**
 * Created by PhpStorm.
 * User: zhengzifan
 * Date: 2017/3/16
 * Time: 10:10
 */

namespace app\admin\controller;

class IndexController extends CommonController {
    public function index(){
        $system_config=db('System')->where(['id'=>['>','0']])->find();
        $this->assign(['domain'=>$this->request->url(true),'system_config'=>$system_config]);
        return $this->fetch('index');
    }

    public function right(){
        $start_time = input('start_time') ? strtotime(input('start_time')) : strtotime(date('Y-m-d',strtotime('-1 month')));
        $end_time = input('end_time') ? strtotime(input('end_time')) : strtotime(date('Y-m-d',time())) + 24 * 3600 -1;
        $where['id']=['>',0];
        $where['create_time'] = ['>=',$start_time];
        $where['create_time'] = ['<=',$end_time];
        $store_ids = model('AdminStore')->where(['admin_id'=>session('admin')['id']])->column('store_id');
        if(!empty($store_ids) && count($store_ids)>0){
            $where['store_id'] = ['in',$store_ids];
        }

        $drs = model('OrderCar')->where($where)->select();
        $list = [];
        for($i=$start_time;$i<=$end_time;$i=$i+(24 * 3600)){
            $time = date('Y-m-d',$i);
            $count = 0;
            $amount = 0;
            foreach($drs as $dr){
                if($time==date('Y-m-d',strtotime($dr['create_time']))){
                    $count = $count+1;
                    $amount =$amount+$dr['amount'];
                }
            }
            $item['time'] = $time;
            $item['count'] = $count;
            $item['amount'] = $amount;
            $list[] = $item;
        }
        
        $topData['amount'] =0.00;
        $topData['order'] =0;
        $topData['amountSum'] =0.00;
        $topData['orderCount'] =0;
        $res['time'] = [];
        $res['order'] = [];
        $res['amount'] =[];
        foreach($list as $item){
            if($item['time'] == date('Y-m-d',time())){
                $topData['amount'] = $topData['amount']+$item['amount'];
                $topData['order'] = $topData['order']+$item['count'];
            }
            $topData['amountSum'] = $topData['amountSum']+$item['amount'];
            $topData['orderCount'] = $topData['orderCount']+$item['count'];
            $res['time'][] = $item['time'];
            $res['order'][] = $item['count'];
            $res['amount'][] = $item['amount'];
        }
        $this->assign([
            'res' => json_encode($res),
            'topData' => $topData
        ]);
        return $this->fetch('right');
    }

    public function getNewOrder(){
        if(model('Order')->where(['order_status'=>0, 'pay_status'=>1])->find()){
            $this->ajaxReturn(['result'=>true]);
        }else{
            $this->ajaxReturn(['result'=>false]);
        }
    }
}